package mule.view;

//<editor-fold defaultstate="collapsed" desc="Imports">
import javafx.beans.property.IntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ChoiceBoxBuilder;
import javafx.scene.control.LabelBuilder;
import javafx.scene.layout.BorderPaneBuilder;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.RegionBuilder;
import javafx.scene.layout.StackPaneBuilder;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import mule.model.GameModel;
import mule.model.enums.Difficulty;
import mule.model.enums.MapType;
//</editor-fold>

/**
 *
 * @author stephen
 */
public class NewGameOptionView extends View
{
    GameModel model = GameModel.getInstance();
    ChoiceBox<Integer> playerNumChoice;
    ChoiceBox<Difficulty> difficultyChoice;
    ChoiceBox<MapType> mapTypeChoice;
    Button continueButton;
    Scene newGameOptionScreen;

    public Scene getScreen() {
        return createGameOptionScreen();
    }
    
    private Scene createGameOptionScreen()
    {
        newGameOptionScreen = SceneBuilder.create()
            .width( SCREEN_WIDTH )
            .height( SCREEN_HEIGHT )
            .root(
                StackPaneBuilder.create()
                    .children(
                        RegionBuilder.create()
                        .style( "-fx-background-image: url('mule/view/images/MULE_start_bg2.png');" )
                        .build(),
                        BorderPaneBuilder.create()
                        .center( 
                            optionGrid() 
                        )
                        .bottom( 
                            continueButton() 
                        )
                        .build()
                    )
                .build()
            )
        .build();

        return newGameOptionScreen;
    }

    private Node optionGrid()
    {
        ObservableList<Integer> playerCounts = FXCollections.observableArrayList( 2, 3, 4 );

        playerNumChoice = ChoiceBoxBuilder.<Integer>create()
            .items( playerCounts )
        .build();
        playerNumChoice.getSelectionModel().selectFirst();

        HBox playerNumBox = HBoxBuilder.create()
            .alignment( Pos.CENTER_LEFT )
            .spacing( 20 )
            .children(
                LabelBuilder.create()
                    .text( "Number of Players" )
                .build(),
                playerNumChoice
            )
        .build();


        difficultyChoice = ChoiceBoxBuilder.<Difficulty>create()
            .items( model.difficulties )
        .build();
        difficultyChoice.getSelectionModel().selectFirst();

        HBox difficultyBox = HBoxBuilder.create()
            .alignment( Pos.CENTER_LEFT )
            .spacing( 20 )
            .children(
                LabelBuilder.create()
                    .text( "Game Difficulty" )
                .build(),
                difficultyChoice
            )
        .build();


        mapTypeChoice = ChoiceBoxBuilder.<MapType>create()
            .items( model.mapTypes )
        .build();
        mapTypeChoice.getSelectionModel().selectFirst();
        mapTypeChoice.setValue(MapType.RANDOM);
        
        HBox mapTypeBox = HBoxBuilder.create()
            .alignment(Pos.CENTER_LEFT)
            .spacing( 20 )
            .children(
                LabelBuilder.create()
                    .text( "Map Type" )
                .build(),
                mapTypeChoice
            )
        .build();

        GridPane optionGrid = new GridPane();
        optionGrid.hgapProperty().set(20);
        optionGrid.vgapProperty().set(20);
        optionGrid.alignmentProperty().set(Pos.CENTER);
        optionGrid.prefHeightProperty().set( SCREEN_HEIGHT / 2 );
        optionGrid.prefWidthProperty().set( SCREEN_WIDTH / 2 );
        optionGrid.add( playerNumBox,       0, 0);
        optionGrid.add( difficultyBox,      0, 1 );
        optionGrid.add( mapTypeBox,         0, 2 );

        return optionGrid;
    }

    private Node continueButton()
    {
        continueButton = ButtonBuilder.create()
            .text( "Continue to Game" )
        .build();

        VBox continueBox = VBoxBuilder.create()
            .alignment( Pos.CENTER )
            .children(
                continueButton
            )
        .build();

        return continueBox;
    }

    public void addContinueListener( EventHandler event ) {
        continueButton.onActionProperty().set( event );
    }
    
    public int getPlayerCount()
    {
        return playerNumChoice.getValue();
    }

    public Difficulty getGameDifficulty()
    {
        return difficultyChoice.getValue();
    }

    public MapType getMapType()
    {
        return mapTypeChoice.getValue();
    }

}
